package com.samebuilding.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.samebuilding.domain.Activity;
import com.samebuilding.domain.ActivityBigtype;
import com.samebuilding.service.ActivityService;
import com.samebuilding.service.ActivityTypeService;
import com.samebuilding.util.PaginationUtil;

public class GetActivityByTypeAction extends DispatchAction {

	
	private ActivityService activityService;
	private ActivityTypeService activityTypeService;
	

	public ActivityTypeService getActivityTypeService() {
		return activityTypeService;
	}


	public void setActivityTypeService(ActivityTypeService activityTypeService) {
		this.activityTypeService = activityTypeService;
	}


	public ActivityService getActivityService() {
		return activityService;
	}


	public void setActivityService(ActivityService activityService) {
		this.activityService = activityService;
	}


	public ActionForward small(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		int limit = 5;
		
		int cur = 0;
		
		if(request.getParameter("cur") != null) {
			cur = NumberUtils.createInteger(request.getParameter("cur"));
		}
		
		int start = cur * limit;
		
		List<ActivityBigtype> typeList = this.activityTypeService.findAllBigType();
		
		int smallType = NumberUtils.createInteger(request.getParameter("small"));
		List<Activity> list = this.activityService.findActivityBySmallType(smallType, start, limit);
		
		int count = this.activityService.findCountBySmallType(smallType);
		
		String url = PaginationUtil.getPaginationUrl(cur, count, limit, "", request);
		
		request.setAttribute("list", list);
		request.setAttribute("typeList", typeList);
		request.setAttribute("url", url);
		return mapping.findForward("getActivityByType");
	}
	
	public ActionForward big(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		int limit = 5;
		
		int cur = 0;
		
		if(request.getParameter("cur") != null) {
			cur = NumberUtils.createInteger(request.getParameter("cur"));
		}
		
		int start = cur * limit;
		
		List<ActivityBigtype> typeList = this.activityTypeService.findAllBigType();
		
		int big = NumberUtils.createInteger(request.getParameter("big"));
		List<Activity> list = this.activityService.findActivityByBigType(big, start, limit);
		
		int count = this.activityService.findCountByBigType(big);
		
		String url = PaginationUtil.getPaginationUrl(cur, count, limit, "", request);
		
		request.setAttribute("typeList", typeList);
		request.setAttribute("list", list);
		request.setAttribute("url", url);
		
		return mapping.findForward("getActivityByType");
	}

	
	
}
